"""
@author: 江同学呀
@file: random_score.py
@date: 2025/1/13 20:50
@desc: 
"""
from typing import Union, Dict

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField


class RandomScore(_Base):
    """
    随机分数

    The random_score generates scores that are uniformly distributed from 0 up to but not including 1. By default, it
    uses the internal Lucene doc ids as a source of randomness, which is very efficient but unfortunately not
    reproducible since documents might be renumbered by merges.
    In case you want scores to be reproducible, it is possible to provide a seed and field. The final score will then
    be computed based on this seed, the minimum value of field for the considered document and a salt that is computed
    based on the index name and shard id so that documents that have the same value but are stored in different indexes
    get different scores. Note that documents that are within the same shard and have the same value for field will
    however get the same score, so it is usually desirable to use a field that has unique values for all documents.
    A good default choice might be to use the _seq_no field, whose only drawback is that scores will change if the
    document is updated since update operations also update the value of the _seq_no field.

    random_score生成的分数从 0 到 1 均匀分布，但不包括 1。默认情况下，它使用内部 Lucene 文档 ID 作为随机性来源，这非常有效，但遗憾的是
    不可重现，因为文档可能会通过合并重新编号。
    如果您希望分数是可重复的，则可以提供种子和字段。然后，将根据此种子、所考虑文档的 field 最小值以及根据索引名称和分片 ID 计算的盐来计算最
    终分数，以便具有相同值但存储在不同索引中的文档获得不同的分数。请注意，位于同一分片中且具有相同字段值的文档将获得相同的分数，因此通常需要使
    用对所有文档具有唯一值的字段。一个好的默认选择可能是使用 _seq_no 字段，其唯一的缺点是，如果文档更新，分数会发生变化，因为更新操作也会更
    新 _seq_no 字段的值。

    :param seed:
    :param field:
    """
    def __init__(self, seed: int = None, field: Union[str, _BaseField] = None):
        super().__init__()
        self._seed: int = seed
        self._field: str = field
        return

    def _build(self) -> Dict:
        return {
            "seed": self._seed,
            "field": self._field
        }
